<?php

class Label{
	var $text ;
	var $tojs = false ;

	function Label($text,$tojs){
		$this->text = $text;
		if(isset($tojs)){
			$this->tojs = $tojs;
		}
	}
}

$labels = array();
$labelIndex = 0 ;

$client_lang = SessionUtils::deep_get("client_lang");
if($client_lang == ""){
	if(isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])){
		$client_lang = $_SERVER["HTTP_ACCEPT_LANGUAGE"] ;
		$matrix = explode(",",$client_lang) ;
		$matrix = explode("-",$matrix[0]) ;
		$client_lang = $matrix[0] ;
	}else{
		$client_lang = "zh";
	}
}

$folder = "../lang/";
$languageFile = $folder . $client_lang . ".php";

if(!file_exists($languageFile)){
	$languageFile = $folder . "en.php";
}

$undifinedLabels = array();
function label($key){
	if($key == ""){
		return "" ;
	}

	global $undifinedLabels ;
	global $client_lang, $titles ;

	if(is_array($titles) && array_key_exists($key,$titles)){
		$value = $titles[$key] ;
		if(gettype($value) == "object"){
			return $value->text  ;
		}
		return $value ;
	}else{
		array_push($undifinedLabels, $key) ;
		return "??.$key($client_lang).??";
	}
}


function addVariables(){
	global $undifinedLabels ,$titles;
	global $languageFile; 

	if(count($undifinedLabels) == 0 || !is_array($titles)){
		return ;
	}

	$code = "";
	for($i=0;$i<count($undifinedLabels);$i++){
		$temp = $undifinedLabels[$i];
		$titles[$temp] = str_replace(".","_",$temp);
		$code .= '$labels["'.$temp.'"] = "";'."\r\n";
	}

	$file = $_SERVER["DOCUMENT_ROOT"] . "/common/i18n/zh/unknow_labels_tmp.php"; 
	rewrite_file($file,"<?php\r\n".$code."\r\n?>","utf-8",false) ;
}

function putLabelsIntoJs(){
	global $titles , $labelIndex , $configs ;
	
	
	$string = "";
	$index = 0 ;
	while(list($key,$value) = each($titles)){
		if($index++ < $labelIndex){
			continue ;
		}
		if($key == null || $value == ""){
			continue ;
		}
		$key = strtolower($key);
		
		if(gettype($value) == "object"){
			if($value->tojs){
				$txt = $value->text ;
				$txt = str_replace("'","&#39;",$txt);

				$string .= "\r\nlabels.put('$key','$txt'); ";
			}
		}
	}
	$labelIndex = $index ;
	$mdfive = md5($string);
	$file = getCurrentJsLabelFile($mdfive) ;
	$file_session = str_replace($_SERVER["DOCUMENT_ROOT"],"../../",$file);
	set_session("last_js_file",$file_session);

	if(file_exists($file)){
		return ;
	}

	rewrite_file($file,$string,"utf-8",true);
}

function getCurrentJsLabelFile($mdfive=""){
	global $sessionLocaleId ;
	$path = $_SERVER["DOCUMENT_ROOT"] ."/system/js_labels/" ;
	mkdirs($path);
	return $path . $sessionLocaleId . str_replace("/","_",$_SERVER["SCRIPT_NAME"]) . "_" . $mdfive . ".js" ;
}

function getCurrentJsScrHtml(){
	global $_SERVER ;
	$file = getCurrentJsLabelFile() ; 
	$flag = md5($_SERVER["SCRIPT_NAME"]);
	return "";
//	return "<script language='javascript' src='/framework/common/page_labels.php?version=$flag'></script>";
}

function listToLabelMatrix($list){
	$temp = array();
	for($i=0;$i<count($list);$i++){
		$key = $list[$i];
		$temp[$key] = label($key);
	}
	return $temp ;
}

function hashToLabelMatrix($hash){
	$temp = array();
	$keys = array_keys($hash);
	for($i=0;$i<count($keys);$i++){
		$key = $keys[$i];
		$temp[$key] = label($hash[$key]);
	}
	return $temp ;
}

$sessionLocaleId = mget($configs,"default_locale",0,"zh") ;
$sessionLocaleId = get_session("session_locale_id",$sessionLocaleId) ;

